layui.config({
    base: basePath+'/layui/lay/modules/'
}).extend({
    eleTree:'eleTree',
    formSelects:'formSelects-v4'
}).use(['eleTree',"form","table",'formSelects'], function() {
    layui.link(basePath+"/layui/css/eleTree.css");
    var $ = layui.jquery;
    var eleTree  = layui.eleTree;//很重要
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var formSelects = layui.formSelects;

    formSelects.on('roleType', function(id, vals, val, isAdd, isDisabled){
        table.reload("role-list",{where:{roleType:val.value}})
    });

    var tableOption = {
        elem: '#role-list'
        ,id: 'role-list'
        ,url: basePath +"/sysRole/index"
        ,where:{roleType:$("#roleType").val()}
        ,method:"post"
        ,title:"角色清单"
        ,cols: [[
            {title: '选择', width:80,type:"radio",fixed:'left'}
            ,{field:'ROLE_NAME', title: '角色名称',edit:"text" }
            ,{field:'ROLE_NO', title: '编号' ,edit:"text"  }
            ,{field:'CREATOR', title: '创建人'  }
        ]]
       ,height: 'full-100'
    };

    table.render(tableOption)

    table.on('edit(role-list)',function(obj){

        var param = {id:obj.data.ID,field:obj.field,value:obj.value,token:token};
        var url = basePath+"/sysRole/update";
        $.post(url,param,function(rst){
            if(rst.code!=0  ){
                layer.msg(rst.msg)
            }
        },"json")
    })


    var radioData ;
    table.on("radio(role-list)",function(obj){
       // console.log(obj)
        radioData = obj.data;
        var roleType = obj.data.ROLE_TYPE
        var url = basePath+"/organization/query"
        if(roleType != "DATA_ROLE"){
            url = basePath + "/function/query" ;
        }

        $("#data-list").css("height",$("#role-form-dom").height()-40)
        eleTree.render({
            elem: '#data-list',
            url:url ,
            type: "post",
            where:{token:token,roleType:roleType,roleId:obj.data.ID},
            showCheckbox: true,
            //contextmenuList: ["add" ],
            //drag: true,
            accordion: false
        });
    })

    eleTree.on("checkbox(data-list)",function(obj) {
        //console.log(radioData)
        var roleId = radioData.ID;
        var currentData = obj.currentData;
        var param = {
            dataId:currentData.id,roleId:roleId,
            token:token,
            checked:currentData.checked||false};
        //console.log(param)
        var url = basePath+"/sysRole/updateRoleData";
        $.post(url,param,function(rst){
            if(rst.code!=0  ){
                layer.msg(rst.msg)
            }
        },"json")
    })

    $("#role-data-add").click(function(){
        console.log(eleTree.checkedData("#data-list"));
    })


    form.on("submit(role-add)",function(data){
        var param = data.field;
        param.token = token;

        var roleTypeName = param.roleType == "FUNC_ROLE"?"功能角色":"数据角色"

        layer.prompt({
            value: 'New Role Name',
            title: '<i class="layui-icon layui-icon-note"></i> 新增'+roleTypeName
            //area: ['800px', '350px'] //自定义文本域宽高
        }, function(value, index, elem){
            //alert(value); //得到value
            var roleName = $.trim(value) || "New Role Name";
            param.roleName = roleName;
            var url = basePath +"/sysRole/add";
            $.getJSON(url,param,function(data){

                if(data.code == 0 ){
                   table.reload("role-list")
                }
                layer.msg(data.msg)
                //return false
            })

            layer.close(index);
        });
        return false
    });

});